package cn.mesmile.shiro.module.user.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.mesmile.shiro.module.role.dto.RoleVo;
import cn.mesmile.shiro.module.role.mapper.RoleMapper;
import cn.mesmile.shiro.module.user.dto.UserVo;
import cn.mesmile.shiro.module.user.entity.User;
import cn.mesmile.shiro.module.user.mapper.UserMapper;
import cn.mesmile.shiro.module.user.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import lombok.AllArgsConstructor;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zb
 * @date 2021/02/04 15:09
 */
@AllArgsConstructor
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS, rollbackFor = Exception.class)
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    private final UserMapper userMapper;

    private final RoleMapper roleMapper;

    @Override
    public UserVo findAllUserInfoByUsername(String username) {
        User user = findUserInfoByUsername(username);
        List<RoleVo> roleByRoleId = roleMapper.findRoleByRoleId(user.getId());
        if (roleByRoleId == null){
            roleByRoleId = new ArrayList<>();
        }
        UserVo userVo = new UserVo();
        BeanUtils.copyProperties(user, userVo);
        userVo.setRoleList(roleByRoleId);
        return userVo;
    }

    @Override
    public User findUserInfoByUsername(String username) {
        LambdaQueryWrapper<User> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(StrUtil.isNotEmpty(username), User::getUsername, username);
        User user = getOne(lambdaQueryWrapper);
//        Assert.isTrue(user != null, "用户名为： "+ username +" 的用户不存在！");
        return user;
    }

}
